<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" width="40%" :visible="show" :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="绑定工牌" prop="cardNum">
            <el-input v-model="form.cardNum" placeholder="请输入工牌"></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { cardList } from '@/api/card';
import { memberEditByCheck } from '@/api/person';
export default {
  props: {
    /* 显示隐藏 */
    show: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: null
    }
  },
  watch: {
    item: {
      handler(val) {
        if (val?.id) {
          this.form = val || {};
        } else {
          this.form = {};
        }
      },
      immediate: true
    },
    show: {
      handler(val) {
        val && this.resetForm('form');
      },
      immediate: true
    }
  },
  data() {
    return {
      form: {},
      rules: {
        cardNum: [
          {
            required: false,
            message: '请输入内容',
            trigger: ['change', 'input']
          }
        ]
      }
    };
  },
  methods: {
    cardList,
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          memberEditByCheck({
            id: this.form.id,
            cardNum: this.form.cardNum
          }).then((res) => {
            // 验证成功-新增或者编辑
            this.$emit('change', this.form);
            this.close();
          });
        }
      });
    },
    close() {
      this.form = {};
      this.$emit('update:show', false);
    }
  }
};
</script>
